package com.lianzhihui.romeex.ui.mianfragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.EcologicalBannerAdapter;
import com.lianzhihui.romeex.adapter.EcologicalFragmentAdapter;
import com.lianzhihui.romeex.bean.BannerBean;
import com.lianzhihui.romeex.bean.EcologicalBean;
import com.lianzhihui.romeex.bean.EcologicalLisrBean;
import com.lianzhihui.romeex.mvp.presenter.EcologicalPresnterImp;
import com.lianzhihui.romeex.mvp.view.EcologicalView;
import com.lianzhihui.romeex.net.APIConstant;
import com.lianzhihui.romeex.ui.base.BaseFragment;
import com.lianzhihui.romeex.ui.base.CustomWebActivity;
import com.lianzhihui.romeex.ui.base.WebViewActivity;
import com.lianzhihui.romeex.ui.ipfs.IPFSActivity;
import com.lianzhihui.romeex.ui.ecological.NodeBuyActivity;
import com.lianzhihui.romeex.utils.IntentUtils;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.utils.UserUtils;
import com.lianzhihui.romeex.widget.ZFEmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.ecological
 * @ClassName: EcologicalFragment
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/10/26 15:13
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/10/26 15:13
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class EcologicalFragment extends BaseFragment implements OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemClickListener, EcologicalView {

    // the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    @BindView(R.id.banner_layout)
    Banner banner;
    @BindView(R.id.myRecyclerView)
    ZFEmptyRecyclerView myRecyclerView;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    @BindView(R.id.status_bar_id)
    View statusBarId;
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.node)
    RelativeLayout node;
//    EcologicalBean myBalance;

    EcologicalFragmentAdapter messageTransferAdapter;
    private EcologicalPresnterImp ecologicalPresnterImp;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ecological;
    }

    @Override
    protected void initView() {

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarId.getHeight());
        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(MyApplication.mContext);
        statusBarId.setLayoutParams(layoutParams);
        useBanner();
        includeTopIvBack.setVisibility(View.GONE);
        includeTopTvTitle.setText(getResources().getText(R.string.text_romeex_ecological));
        mySmartRefreshLayout.setEnableLoadMore(false);
        mySmartRefreshLayout.setEnableRefresh(false);
//        mySmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        ArrayList<EcologicalLisrBean> objects = new ArrayList<>();
//        objects.add(new EcologicalLisrBean("IPFS算力池", "", R.drawable.icon_computingpool, ""));
        objects.add(new EcologicalLisrBean("RICE大学", "", R.drawable.icon_romeuniversity, APIConstant.H5_RICE1));
        objects.add(new EcologicalLisrBean("RICE官网", "", R.drawable.icon_officialwebsite, APIConstant.H5_RICE2));
        objects.add(new EcologicalLisrBean("区块浏览器", "", R.drawable.icon_blockexplorer, APIConstant.H5_RICE3));
        objects.add(new EcologicalLisrBean("销毁池", "", R.drawable.icon_destroypool, APIConstant.H5_RICE4));
        objects.add(new EcologicalLisrBean("竞技池", "", R.drawable.icon_competitivepool, APIConstant.H5_RICE5));
        objects.add(new EcologicalLisrBean("开发者中心", "", R.drawable.icon_developercenter, APIConstant.H5_RICE6));
        objects.add(new EcologicalLisrBean("RICE借贷", "", R.drawable.icon_romeloan, APIConstant.H5_RICE7));

        String url8 = APIConstant.H5_RICE8 + UserUtils.getCurrentUserAddress() + "&os=android&signAsStr=rome";
//        String url8 = APIConstant.H5_RICE8 + "13yNMZ2RSqM2ho7MRAFFMsJdrcjZWWxTQ3" + "&os=Android&signAsStr=rome";
        objects.add(new EcologicalLisrBean("智能量化工具", "", R.drawable.rex_recharge, url8));
        objects.add(new EcologicalLisrBean("IPFS算力池", "", R.drawable.icon_computingpool, ""));

        messageTransferAdapter = new EcologicalFragmentAdapter(getActivity(), objects);
        messageTransferAdapter.setOnItemClickListener(this);
        myRecyclerView.setLayoutManager(linearLayoutManager);
        myRecyclerView.setAdapter(messageTransferAdapter);
        myRecyclerView.showEmptyView(objects.size(), 1);
        ecologicalPresnterImp = new EcologicalPresnterImp(getActivity(), this);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        ecologicalPresnterImp.requestMinnerMyBalance();
    }

    public static EcologicalFragment newInstance() {
        EcologicalFragment fragment = new EcologicalFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, "");
        args.putString(ARG_PARAM2, "");
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    public void useBanner() {

        List<BannerBean> bannerData = new ArrayList<>();
        BannerBean bannerBean = new BannerBean();
        bannerBean.setPicRes(R.drawable.shengtai_baner_one);
        BannerBean bannerBean1 = new BannerBean();
        bannerBean1.setPicRes(R.drawable.shengtai_baner_two);
        bannerData.add(bannerBean);
        bannerData.add(bannerBean1);

        EcologicalBannerAdapter adapter = new EcologicalBannerAdapter(getActivity(), bannerData);
        banner.setAdapter(adapter);
        banner.setIndicator(new CircleIndicator(getActivity()));

    }


    @Override
    public void onResume() {
        super.onResume();
        banner.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.stop();
    }


    @OnClick({R.id.node})
    public void onClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.node:
                    IntentUtils.JumpTo(NodeBuyActivity.class);
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        EcologicalLisrBean item = messageTransferAdapter.getItem(position);
//        if (position == 0) {
//            Toast.makeText(getContext(), R.string.text_will_be_open, Toast.LENGTH_SHORT).show();
////            IntentUtils.JumpTo(IPFSActivity.class);
//            return;
//        }

        if (item.getTitle().equals("智能量化工具")) {
            Intent intent2 = new Intent(activity, WebViewActivity.class);
            intent2.putExtra("url", messageTransferAdapter.getItem(position).getUrl());
            intent2.putExtra("title", messageTransferAdapter.getItem(position).getTitle());
            intent2.putExtra("isHideTitle", true);
            startActivity(intent2);
            return;
        }
//        if (item.getTitle().equals("IPFS算力池")) {
//            Intent intent = new Intent(activity, IPFSActivity.class);
//            startActivity(intent);
//            return;
//        }
        if (TextUtils.isEmpty(messageTransferAdapter.getItem(position).getUrl())) {
            Toast.makeText(getContext(), R.string.text_will_be_open, Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent2 = new Intent(activity, CustomWebActivity.class);
        intent2.putExtra("url", messageTransferAdapter.getItem(position).getUrl());
        intent2.putExtra("title", messageTransferAdapter.getItem(position).getTitle());
        intent2.putExtra("isHT", true);
        intent2.putExtra("isHideTitle", false);
        startActivity(intent2);

    }

    @Override
    public void responseMinnerMyBalanceSuccess(EcologicalBean b) {
//        if (isAdded()) {
//            myBalance = b;
//        }
    }

    @Override
    public void responseMinnerTransferSuccess() {

    }

    @Override
    public void setFailure() {

    }
}